import java.lang.*;
import java.util.*;
import java.text.*;

public class GPSTime {
    long week;// = 1627;    //gps week
    long sec;//  = 109150;  //gps seconds
    long timeInMilliS;

    //conctructor option
    public GPSTime() {
        week = 1627;
        sec  = 109150;
    }
        
    public GPSTime(long w, long s) {
        week = w;
        sec = s;
        System.out.println("IN GPSTime()");
        //gpsWeek2Utc(week,sec);
    }

    public GPSTime(long w) {
        week = w;
        sec = 0;
        //gpsWeek2Utc(week,sec);
    }

    public static void main(String[] args) {
        long value = utc2GpsWeek();
        //System.out.println("GPS week:"+week+" GPS Seconds:"+sec);
        //long time = gpsWeek2Utc(week,sec);
        //long timeAnswer = utc2GpsWeek(time);
    }

    // Return the current set time in a date string format
    public String dateString() {
        Date javaDate = new Date(timeInMilliS);
        //c.setTime(javaDate);
        SimpleDateFormat formatDate = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        String dateString = formatDate.format( javaDate );

        System.out.println("Date String "+dateString);
        return dateString;
    }

    private static long gpsWeek2Utc(long week, long sec) {
        // GPS Start epoch
        GregorianCalendar gc1 = new GregorianCalendar(1980,Calendar.JANUARY,6,0,0,0);
        //GregorianCalendar c = new GregorianCalendar();

        long elaps = gc1.getTimeInMillis();
        System.out.println("Elapsed Seconds: " + elaps);

        long time = elaps + week*7*24*3600*1000 + sec*1000;
        System.out.println("Time "+time);

        //Date javaDate = new Date(time);
        //c.setTime(javaDate);
        //SimpleDateFormat formatDate = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        //String dateString = formatDate.format( javaDate );
        //System.out.println("Date String:"+dateString);

        return time;
    }

    public static long utc2GpsWeek() {
        GregorianCalendar gpsC = new GregorianCalendar(1980,Calendar.JANUARY,6,0,0,0);
        GregorianCalendar c = new GregorianCalendar();

        long time = c.getTimeInMillis();
        long gpsStart = gpsC.getTimeInMillis();
        long elapsed  = time - gpsStart;

        long gpsWeek = elapsed/(1000*86400*7);
        long gpsSeconds  = (elapsed - gpsWeek*7*24*3600*1000)/1000;
        long gpsDow  = gpsSeconds / 86400;

        System.out.println("gpsweek->   "+gpsWeek);
        System.out.println("gpsDOW->    "+gpsDow);
        System.out.println("gpsSecs->   "+gpsSeconds);

        return elapsed;
    }

    public static long utc2GpsWeek(long time) {
        GregorianCalendar gpsC = new GregorianCalendar(1980,Calendar.JANUARY,6,0,0,0);
        GregorianCalendar c = new GregorianCalendar();

        long gpsStart = gpsC.getTimeInMillis();
        long elapsed  = time - gpsStart;

        long gpsWeek = elapsed/(1000*86400*7);
        long gpsSeconds  = (elapsed - gpsWeek*7*24*3600*1000)/1000;
        long gpsDow  = gpsSeconds / 86400;

        Date javaDate = new Date(time);
        c.setTime(javaDate);
        SimpleDateFormat formatDate = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        String dateString = formatDate.format( javaDate );

        System.out.println("Date String "+dateString);
        System.out.println("GPS WEEK: "+gpsWeek);
        System.out.println("GPS DoW: "+gpsDow);
        System.out.println("GPS Seconds: "+gpsSeconds);
        //String value = "Value";
        return elapsed;
    }


}
